package com.odm.communitymanage.ui.mine.MyIdle

import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.Toast
import androidx.lifecycle.Observer
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.bumptech.glide.Glide
import com.jeremyliao.liveeventbus.LiveEventBus
import com.odm.communitymanage.R
import com.odm.communitymanage.base.BaseFragment
import com.odm.communitymanage.core.Constants
import com.odm.communitymanage.core.bean.MyIdleData
import com.odm.communitymanage.core.bean.NetCallBack
import com.odm.communitymanage.core.net.ApiService
import com.odm.communitymanage.ui.ImageDetailActivity
import com.odm.communitymanage.util.GlideUtils
import com.rxjava.rxlife.lifeOnMain
import io.reactivex.disposables.Disposable
import kotlinx.android.synthetic.main.fragment_my_idle_detail.*
import kotlinx.android.synthetic.main.item_idle_mine.*
import rxhttp.wrapper.param.RxHttp

/**
 * @description: 我的闲置详情
 * @author: ODM
 * @date: 2020/4/18
 */
class MyIdleDetailFragment : BaseFragment() {

    private var disposable : Disposable?= null

    private var idleId : Int = 0



    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        registerLiveEvent()
    }

    override fun onDestroyView() {
        super.onDestroyView()
        if(disposable?.isDisposed == false){
            disposable?.dispose()
        }
    }

    override fun initViews() {
        //交易成功按钮 点击事件
        btnConfirm_MyIdleDetail.setOnClickListener {
            completeIdle()
        }

    }

    override fun getLayoutId(): Int {
        return R.layout.fragment_my_idle_detail
    }


    private fun registerLiveEvent(){
        LiveEventBus.get(Constants.EVENT_MY_IDLE_DETAIL , MyIdleData::class.java)
            .observeSticky(this  , object : Observer<MyIdleData> {
                override fun onChanged(t: MyIdleData ?) {
                    loadMyIdleDetail(t)
                }
            })
    }

    private fun loadMyIdleDetail(data : MyIdleData ?){
        
        val contactInfo = data?.userName+ " "+data?.phone
        tvContactInfo_MyIdleDetail.text = contactInfo
        tvTime_MyIdleDetail.text = data?.time
        tvDescription_MyIdleDetail.text  = data?.description
        tvRemark_MyIdleDetail.text  = data?.remarks
        val price =   "￥" + data?.price
        tvPrice_MyIdlePrice.text = price
        tvName_MyIdleDetail.text = data?.topic

        if(data?.status == 0){
            //说明正在出售中，显示交易成功的按钮
            btnConfirm_MyIdleDetail.visibility = View.VISIBLE
        }else {
            btnConfirm_MyIdleDetail.visibility = View.GONE
        }


        //id状态保存
        idleId = data?.id ?: 0

        // 加载 图片 ，图片数组可能为 null
        data?.imgs?.let {
                for(url in data.imgs){
                    var target : ImageView ?= ivPhoto_0_MyIdleDetail
                    if(ivPhoto_0_MyIdleDetail.visibility != View.VISIBLE){
                        target = ivPhoto_0_MyIdleDetail
                    }else if(ivPhoto_1_MyIdleDetail.visibility != View.VISIBLE) {
                        target = ivPhoto_1_MyIdleDetail
                    }
                    target?.apply {
                        visibility = View.VISIBLE
                        //加载图片
                        GlideUtils
                            .getInstance()
                            .loadImageUrl(requireContext(),url ,target)
                        //点击事件，跳转加载大图Activity
                        setOnClickListener {
                            LiveEventBus
                                .get(Constants.IMAGE_URL)
                                .post(url)
                            ActivityUtils.startActivity(ImageDetailActivity::class.java)
                        }
                    }
                }
        }

        //加载系统默认头像
        setNameAvatar(data?.userName?:"无" , ivAvatar_MyIdleDetail)
    }

    /**
     * 交易完成，post请求
     */
    private fun completeIdle(){
        disposable = RxHttp.postJson(ApiService.COMPLETE_IDLE)
            .add("id", idleId)
            .asClass(NetCallBack::class.java)
            .lifeOnMain(this)
            .subscribe({
                if(it.code == 1){
                    showSuccessSnack(getString(R.string.post_success))
                }else {
                    ToastUtils.showShort(it.message)
                }
            },{
                ToastUtils.showShort(it.message)
                it.printStackTrace()
            })
    }




}